﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Diagnostics;
using System.Text;
using ReliableAppClasses;

namespace ReliableAppHostApp
{
	class Program
	{
		static void Main(string[] args)
		{
			//TestLogExtensions();
			//CreatePerformanceCategory();
			//TestPerformanceCounter();
            //TestBase64Encoding();
		}

		private static void TestLogExtensions()
		{
			var traceFile = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\EFTrace.log";

			using (var context = new NorthwindEntities())
			{
				var cust = from c in context.Customers
				           where c.CompanyName.StartsWith("A") && String.Equals(c.CompanyName, "UK")
				           select c;

				File.AppendAllText(traceFile, cust.ToTraceString());
				Console.WriteLine("Done");
			}
		}

		private static void CreatePerformanceCategory()
		{
			const string categoryName = "MyAppCategory";
			if (!PerformanceCounterCategory.Exists(categoryName))
			{
				PerformanceCounterCategory.Create(categoryName, "Here is the category help.",
				                                  PerformanceCounterCategoryType.SingleInstance, "MyCounter",
				                                  "Here is counter to help.");
				Console.WriteLine("Category created");
			}
			else
			{
				Console.WriteLine("Category already exists");
			}
		}

		private static void TestPerformanceCounter()
		{
			using (var context = new NorthwindEntities())
			{
				var pc = new PerformanceCounter("MyAppCategory", "MyAppCounter", false);
				var customers = from c in context.Customers
								where c.CompanyName.StartsWith("A") && String.Equals(c.Country, "UK")
								select c;
				var items = customers.ToList();
				pc.IncrementBy(items.Count);
				Console.WriteLine("Done");
			}
		}

        private static void TestBase64Encoding()
        {
            var lol = new Byte[] {1, 2, 3, 4, 5};
            var encoded = Convert.ToBase64String(lol);
            var backToBinary = Convert.FromBase64String(encoded);

            Console.WriteLine("Bytes:");
            foreach (var b in lol)
            {
                Console.WriteLine(b);
            }


            Console.WriteLine("\nEncoded:\n{0}\n", encoded);
            Console.WriteLine(String.Format("Sequence equality {0}", lol.SequenceEqual(backToBinary)));
        }
	}
}
